package com.mlm.mlmapp.adapter.view;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.util.Constants;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.adapter.view.listener.DresserDetailClickListener;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.utils.CommonUtils;

@SuppressLint("ValidFragment")
public class HairDresserDetailItemFragment extends Fragment {
	AQuery aq;
	private Context context;
	ImageView iv_collect;
	ImageView iv_sy_img;
	TextView tv_sy_intro;
	TextView tv_collection_count;
	Button btn_appointment;
	DresserDetailClickListener l;
	String mobile;
	int isCollection = 0;

	public HairDresserDetailItemFragment(Context con,
			DresserDetailClickListener l, String mobile) {

		this.context = con;
		this.l = l;
		this.mobile = mobile;
	}

	public void setCollectionStatus(boolean status) {
		if (status) {
			isCollection = 2;
			iv_collect.setImageResource(R.drawable.dresser_defualt_collect);
		} else {
			isCollection = 1;
			iv_collect.setImageResource(R.drawable.dresser_collected);
		}
	}

	public void setCounts(int count) {
		tv_collection_count.setText(count + "");
	}

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.hairdress_detail_item, container,
				false);
		if (aq != null) {
			aq = aq.recycle(view);
		}
		aq = new AQuery(view);

		iv_collect = (ImageView) view.findViewById(R.id.iv_collect);
		iv_sy_img = (ImageView) view.findViewById(R.id.iv_sy_img);
		tv_sy_intro = (TextView) view.findViewById(R.id.tv_sy_intro);
		tv_collection_count = (TextView) view
				.findViewById(R.id.tv_collection_count);
		btn_appointment = (Button) view.findViewById(R.id.btn_appointment);
		Bundle b = getArguments();

		try {

			final JSONObject json = new JSONObject(b.getString(HTTPCons.JSON));
			isCollection = CommonUtils.getJsonInt(
					json,"isCollect");
			tv_sy_intro.setText(CommonUtils.getJsonString(json,
					"infoDescription"));

			tv_collection_count.setText(CommonUtils.getJsonString(json,
					"collectCount"));
			if (isCollection == 1) {
				// 已收藏
				iv_collect.setImageResource(R.drawable.dresser_collected);
			} else {
				// 未收藏
				iv_collect.setImageResource(R.drawable.dresser_defualt_collect);
			}
			aq.id(iv_sy_img).image(
					CommonUtils.getJsonString(json, "hairPhotoUrl"), true,
					true, 0, 0, null, Constants.FADE_IN);
			iv_collect.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					l.onCollectClick(CommonUtils.getJsonString(json, "id"),
							isCollection);
				}
			});
			iv_sy_img.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					l.onItemClicked(CommonUtils.getJsonString(json, "id"));
				}
			});
			btn_appointment.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					l.onAppointmentClick(CommonUtils.getJsonString(json, "id"));
				}
			});

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return view;
	}

}
